package com.itc.bi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.bi.bo.DataSetDirctTreeBO;
import com.itc.bi.entity.DirectoryEntity;
import com.itc.bi.vo.Direcr.DircetDataInfoVO;
import com.itc.bi.vo.bigScreenVO.ProDirDataSetVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author hecai
* @description 针对表【tb_directory(数据目录表)】的数据库操作Mapper
* @createDate 2024-06-14 15:56:56
* @Entity com.itc.bi.com.itc.bi.Directory
*/
public interface DirectoryMapper extends BaseMapper<DirectoryEntity> {

    /**
     * 数据集创建导入数据库
     * @param sql
     * @return
     */
    @Select(" ${sql} ")
    Boolean createDataBaseTable(@Param("sql")String sql);

    /**
     * 向数据表插入数据
     * @param sql
     * @return
     */
    @Insert(" ${sql} ")
    Boolean insertDataBaseTable(@Param("sql")String sql);

    /**
     * 大屏获取数据集分页
     * @param page
     * @param dataSetName
     * @return
     */
    @Select(" SELECT " +
            "  a.dt_id AS id, " +
            "  a.name AS dataSetName, " +
            "  a.table_name, " +
            "  b.type  " +
            " FROM " +
            "  tb_directory AS a " +
            "  INNER JOIN tb_direct_data_set AS b ON a.dt_id = b.id  " +
            " WHERE " +
            "  a.is_dir = 1 AND a.table_name IS NOT NULL AND a.is_deleted=0  " +
            " AND a.name like '%${dataSetName}%' ")
    Page<ProDirDataSetVO> getProDirDataSetPage(Page<ProDirDataSetVO> page,
                                               @Param("dataSetName")String dataSetName);


    /**
     * 获取数据集详情
     * @param dtId
     * @return
     */
    @Select(" SELECT " +
            " a.create_by, " +
            " a.create_time, " +
            " a.update_time,a.table_name, " +
            " (select count(*) from tb_data_set_column as b where b.data_set_id=a.dt_id) as columnCount " +
            "FROM " +
            " tb_directory AS a where a.dt_id = ${dtId} ")
    DircetDataInfoVO getDircetDataInfo(@Param("dtId") Long dtId);

    @Select(" select a.dt_id,a.p_id,a.is_dir,a.name,b.type,c.cron " +
            "from tb_directory as a  " +
            "LEFT JOIN tb_direct_data_set as b on a.dt_id=b.id " +
            "LEFT JOIN tb_data_synrecord as c on a.dt_id=c.dt_id " +
            "where a.is_deleted=0  " +
            "ORDER BY a.is_dir asc,a.create_time asc ")
    List<DataSetDirctTreeBO> getDataSetTree();
}




